"""
Case Type   : 共享存储-系统内部工具-dsscmd
Case Name   : dsscmd工具读取dss文件内容之其他可选参数验证
Create At   : 2023/4/12
Owner       : opentestcase019
Description :
    1.读取dss文件内容，指定-s read_size参数为有效值
    2.读取dss文件内容，指定-s read_size参数为无效值
    3.读取dss文件内容，指定-D DSS_HOME参数为正常值
    4.读取dss文件内容，指定-D DSS_HOME参数为异常值
    5.读取dss文件内容，指定-U UDS:socket_domain参数为正常值
    6.读取dss文件内容，指定指定错误的-U UDS:socket_domain参数和权限不足的socket_domain
Expect      :
    1.显示成功
    2.合理报错
    3.显示成功
    4.合理报错
    5.显示成功
    6.合理报错Failed to get uds connection.
History     :
"""
import os
import unittest
from yat.test import Node
from yat.test import macro
from testcase.utils.Logger import Logger
from testcase.utils.Common import Common
from testcase.utils.Constant import Constant


class ShareStorageTool(unittest.TestCase):
    def setUp(self):
        self.logger = Logger()
        self.logger.info(f"-----{os.path.basename(__file__)} start-----")
        self.primary_node = Node('PrimaryDbUser')
        self.common = Common()
        self.constant = Constant()

    def test_sharestorage_tool(self):
        step1 = "-----step1:读取dss文件内容，指定-s read_size参数为有效值;" \
                "expect:显示成功-----"
        self.logger.info(step1)
        self.logger.info("-----获取卷组名-----")
        execute_cmd = f"source {macro.DB_ENV_PATH};dsscmd lsvg"
        self.logger.info(execute_cmd)
        execute_msg = self.common.get_sh_result(self.primary_node, execute_cmd)
        self.assertIn("Succeed to display lsvg info.", execute_msg,
                      "获取卷组名失败")
        vgname = execute_msg.splitlines()[1].split()[0].strip()
        self.logger.info(vgname)
        cmd_list = ["-s 0", "-s 100", " -s '200'"]
        exp = f"filename is +{vgname}/pg_control, offset is 2048."
        for cmd in cmd_list:
            execute_cmd = f"source {macro.DB_ENV_PATH};" \
                f"dsscmd examine -p +{vgname}/pg_control -o 2048 -f c {cmd}"
            self.logger.info(execute_cmd)
            execute_msg = self.common.get_sh_result(self.primary_node,
                                                    execute_cmd)
            if cmd == cmd_list[0]:
                self.assertEquals('', execute_msg, "执行失败" + step1)
            else:
                self.assertIn(exp, execute_msg, "执行失败" + step1)

        step2 = "-----step2:读取dss文件内容，指定-s read_size参数为无效值;" \
                "expect:合理报错-----"
        self.logger.info(step2)
        cmd_list = ['-f s -s -1', '-f h -s 50.41', '-f u -s a',
                    '-f u -s $1', '-f u -s *']
        for cmd in cmd_list:
            execute_cmd = f"source {macro.DB_ENV_PATH};" \
                        f"dsscmd examine -p +{vgname}/pg_control -o 2048 {cmd}"
            self.logger.info(execute_cmd)
            execute_msg = self.common.get_sh_result(self.primary_node,
                                                    execute_cmd)
            if cmd == cmd_list[0]:
                self.assertIn("The read_size should >= 0.",
                              execute_msg, "执行失败" + step2)
            elif cmd == cmd_list[3]:
                self.assertIn("-s should with args.",
                              execute_msg, "执行失败" + step2)
            else:
                self.assertIn("The value of read_size is invalid.",
                              execute_msg, "执行失败" + step2)

        step3 = "-----step3:读取dss文件内容，指定-D DSS_HOME参数为正常值;" \
                "expect:显示成功-----"
        self.logger.info(step3)
        execute_cmd = f"source {macro.DB_ENV_PATH};" \
                      f"dsscmd examine -p +{vgname}/pg_control -o 2048 -f c " \
                      f"-s 128 -D {macro.DSS_INSTANCE}"
        self.logger.info(execute_cmd)
        execute_msg = self.common.get_sh_result(self.primary_node, execute_cmd)
        self.assertIn(exp, execute_msg, "执行失败" + step3)

        step4 = "-----step4:读取dss文件内容，指定-D DSS_HOME参数为异常值;" \
                "expect:合理报错-----"
        self.logger.info(step4)
        execute_cmd = f"source {macro.DB_ENV_PATH};" \
                      f"dsscmd examine -p +data/pg_control -o 2048 -f c " \
                      f"-s 128 -D {macro.DSS_INSTANCE}_error"
        self.logger.info(execute_cmd)
        execute_msg = self.common.get_sh_result(self.primary_node, execute_cmd)
        cfg_path = os.path.join(f"{macro.DSS_INSTANCE}_error", 'cfg',
                                'dss_inst.ini')
        self.assertIn("Failed to load config info!",
                      execute_msg, "执行失败" + step4)
        self.assertIn(f"The file config of {cfg_path} does not exist",
                      execute_msg, "执行失败" + step4)
        execute_cmd = f"chmod 000 {macro.DSS_INSTANCE};" \
                      f"source {macro.DB_ENV_PATH};" \
                      f"dsscmd examine -p +{vgname}/pg_control -o 2048 -f c " \
                      f"-s 128 -D {macro.DSS_INSTANCE};" \
                      f"chmod 700 {macro.DSS_INSTANCE};"
        self.logger.info(execute_cmd)
        execute_msg = self.common.get_sh_result(self.primary_node, execute_cmd)
        log_path = os.path.join(macro.DSS_INSTANCE, 'log')
        self.assertIn(f"Failed to load config info!\n detail reason [32] : "
                      f"The file config of {macro.DSS_INSTANCE}/cfg/dss_inst"
                      f".ini does not exist", execute_msg, "执行失败" + step4)

        step5 = "-----step5:读取dss文件内容，指定-U UDS:socket_domain参数" \
                "为正常值;expect:显示成功-----"
        self.logger.info(step5)
        execute_cmd = f"source {macro.DB_ENV_PATH};" \
                      f"dsscmd examine -p +data/pg_control -o 2048 -f c " \
                      f"-s 128 -U {macro.UDS_PATH}"
        self.logger.info(execute_cmd)
        execute_msg = self.common.get_sh_result(self.primary_node, execute_cmd)
        self.assertIn(exp, execute_msg, "执行失败" + step5)

        step6 = "-----step6:读取dss文件内容，指定指定错误的-U UDS:socket_domain参数和权限不足" \
                "的socket_domain;expect:合理报错Failed to get uds connection.-----"
        self.logger.info(step6)
        execute_cmd = f"source {macro.DB_ENV_PATH};" \
                      f"dsscmd ls -p +{vgname} -U {macro.UDS_PATH}.cfg"
        self.logger.info(execute_cmd)
        execute_msg = self.common.get_sh_result(self.primary_node, execute_cmd)
        self.assertIn(self.constant.failed_to_get_uds_connection,
                      execute_msg, "执行失败" + step6)
        execute_cmd = f"chmod 000 {macro.UDS_PATH[4::]};" \
                      f"source {macro.DB_ENV_PATH};" \
                      f"dsscmd ls -p +{vgname} -U {macro.UDS_PATH};" \
                      f"chmod 600 {macro.UDS_PATH[4::]};"
        self.logger.info(execute_cmd)
        execute_msg = self.common.get_sh_result(self.primary_node, execute_cmd)
        self.assertIn(self.constant.failed_to_get_uds_connection,
                      execute_msg, "执行失败" + step6)

    def tearDown(self):
        self.logger.info("-----无需环境清理-----")
        self.logger.info(f"-----{os.path.basename(__file__)} end-----")
